#include <kwdb/client.h>
#include <iostream>
#include <vector>
#include <string>

int main() {
    kwdb::IngestConfig config;
    config.ingest_endpoint = "127.0.0.1:9090";
    config.timeout_ms = 5000;

    kwdb::KwdbClient client(config);

    // Test query functionality
    std::vector<std::vector<std::string>> rows;

    // Test 1: Count query
    auto status = client.query("SELECT count(*) FROM sdk_bench", rows);
    if (status.ok()) {
        std::cout << "✅ Query test 1 PASS: Count query succeeded" << std::endl;
        std::cout << "   Result: " << (rows.empty() ? "0" : rows[0][0]) << " rows in sdk_bench" << std::endl;
    } else {
        std::cout << "❌ Query test 1 FAIL: " << status.message << std::endl;
    }

    // Test 2: Invalid SQL
    rows.clear();
    status = client.query("INVALID SQL SYNTAX", rows);
    if (!status.ok()) {
        std::cout << "✅ Query test 2 PASS: Invalid SQL correctly rejected" << std::endl;
        std::cout << "   Error: " << status.message << std::endl;
    } else {
        std::cout << "❌ Query test 2 FAIL: Should have failed with invalid SQL" << std::endl;
    }

    // Test 3: Select with limit
    rows.clear();
    status = client.query("SELECT ts, host, usage FROM sdk_bench ORDER BY ts DESC LIMIT 5", rows);
    if (status.ok()) {
        std::cout << "✅ Query test 3 PASS: Select query succeeded" << std::endl;
        std::cout << "   Returned " << rows.size() << " rows" << std::endl;
        for (size_t i = 0; i < rows.size() && i < 3; i++) {
            std::cout << "   Row " << i << ": ";
            for (const auto& val : rows[i]) {
                std::cout << val << " ";
            }
            std::cout << std::endl;
        }
    } else {
        std::cout << "❌ Query test 3 FAIL: " << status.message << std::endl;
    }

    std::cout << "\n=== Query functionality test completed ===" << std::endl;
    return 0;
}